package org.xiaodongwei.jvm.op;

import org.xiaodongwei.jvm.exe.JVMStack;
import tech.medivh.classpy.classfile.bytecode.Bipush;
import tech.medivh.classpy.classfile.bytecode.Instruction;
import tech.medivh.classpy.classfile.bytecode.Sipush;
import tech.medivh.classpy.classfile.constant.ConstantPool;

public class SiPushOpcodeExecute extends OpCodeExecute {

    public SiPushOpcodeExecute(JVMStack jvmStack, ConstantPool constantPool) {
        this.jvmStack = jvmStack;
        this.constantPool = constantPool;
    }


    @Override
    public void execute(Instruction instruction) {
        Sipush sipush = (Sipush) instruction;
        String desc = sipush.getDesc();
        jvmStack.peek().getOperateStack().push(parsePushValue(desc));
    }

    private Object parsePushValue(String desc) {
        if (desc == null || desc.isEmpty()) return null;
        String valString = desc.split(" ")[1];
        Integer integer = Integer.parseInt(valString);
        return integer;
    }
}
